package com.py.credit.service.impl;

import com.py.credit.common.rest.RestServiceResult;
import com.py.credit.dao.CswhDao;
import com.py.credit.dao.LyDao;
import com.py.credit.entity.CswhObj;
import com.py.credit.entity.LyObj;
import com.py.credit.entity.TreeObj;
import com.py.credit.service.LyService;
import com.py.credit.utils.DeptTreeUtil;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.List;
import java.util.Map;

@Service
public class LyServiceImpl implements LyService {

    @Autowired
    private LyDao lyDao;

    @Autowired
    private CswhDao cswhDao;

    @Autowired
    private DeptTreeUtil deptTreeUtil;

    @Override
    public List<TreeObj> selectCswhTreeList(String jclb, String name) {
        return deptTreeUtil.getTree(lyDao.selectCswhTreeList(jclb, name));
    }

    @Override
    public List<TreeObj> selectGgCswhTreeList(String jclb, String name) {
        return deptTreeUtil.getTree(lyDao.selectGgCswhTreeList(jclb, name));
    }

    @Override
    public List<Map<String, String>> selectLyList(String type) {
        return lyDao.selectLyList(type);
    }

    @Override
    public List<TreeObj> selectLyTreeList(String type, String name) {
        return lyDao.selectLyTreeList(type, name);
    }
    @Override
    public List<TreeObj> selectLyTreeListByLy(String type, String name) {
        return lyDao.selectLyTreeListByLy(type, name);
    }
    @Override
    public List<TreeObj> selectLyTreeListByCs(String type, String name) {
        return lyDao.selectLyTreeListByCs(type, name);
    }

    @Override
    public List<TreeObj> selectDeptTreeList(String type, String name) {
        return lyDao.selectDeptTreeList(type, name);
    }
    
    @Override
    public List<TreeObj> selectDeptTreeListByDept(String type, String name) {
        return lyDao.selectDeptTreeListByDept(type, name);
    }
    
    @Override
    public List<TreeObj> selectDeptTreeListByMeas(String type, String name) {
        return lyDao.selectDeptTreeListByMeas(type, name);
    }

    @Override
    public void add(String name, String code, String type, int pId, int rId, int sort) {
        lyDao.add(name, code, type, pId, rId, sort);
    }

    @Override
    public RestServiceResult del(String type, String id) {
        List<TreeObj> treeObjs = selectLyTreeList(type, null);

        if (this.findTreeObj(treeObjs,id)) {
            return new RestServiceResult(RestServiceResult.CODE_UNKNOW, "该分类下有未删除的子节点");
        } else {
            lyDao.del("Y", id);
            return new RestServiceResult(RestServiceResult.CODE_SUCCESS);
        }
    }

    @Override
    public void update(String id, String name, String code, String type, int pId, int rId, int sort) {
        lyDao.update(id, name, code, type, pId, rId, sort);
    }

    @Override
    public int selectSameCode(String id, String name, String code) {
        return lyDao.selectSameCode(id, name, code);
    }

    @Override
    public LyObj selectLy(String id) {
        return lyDao.selectLy(id);
    }

    @Override
    public boolean findTreeObj(List<TreeObj> treeObjs, String id) {
        for (TreeObj treeObj : treeObjs) {
            if (treeObj.getpId().equals(id)) {
                return true;
            }
        }
        return false;
    }

    @Override
    public void excelExport(HttpServletResponse response, String type) {
        queryTableExecle(response, type);
    }

    private void queryTableExecle(HttpServletResponse response, String type) {
        // 生成提示信息
        response.setContentType("application/vnd.ms-excel");
        HSSFWorkbook workbook = null;
        OutputStream fOut = null;
        String codedFileName = "领域措施数据";
        // 进行转码，使其支持中文文件名
        try {
            codedFileName = URLEncoder.encode(codedFileName, "UTF-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        response.setHeader("content-disposition", "attachment;filename=" + codedFileName + ".xls");
        // 产生工作簿对象
        workbook = new HSSFWorkbook();
        // 产生工作表对象
        HSSFSheet sheet = workbook.createSheet();
        workbook.setSheetName(0, "具体领域措施数据");
        HSSFRow row = null;
        HSSFCell cell = null;

        String[] strBT = {"领域编码", "领域名称", "措施编码", "惩戒措施", "适用主体", "措施手段", "法律及政策依据", "奖惩类别"};

        //创建标题行
        row = sheet.createRow(0);
        for (int i = 0; i < strBT.length; i++) {
            cell = row.createCell(i);
            cell.setCellType(HSSFCell.CELL_TYPE_STRING);
            cell.setCellValue(strBT[i]);
        }

        int count = 1;
        List<CswhObj> cswhObjList = cswhDao.selectCswhByLyId(type);

        for (int j = 0; j < cswhObjList.size(); j++) {
            row = sheet.createRow(count);//创建一行
            cell = row.createCell(0);
            cell.setCellType(HSSFCell.CELL_TYPE_STRING);
            cell.setCellValue((String) cswhObjList.get(j).getLybm());

            cell = row.createCell(1);
            cell.setCellType(HSSFCell.CELL_TYPE_STRING);
            cell.setCellValue((String) cswhObjList.get(j).getLyName());

            cell = row.createCell(2);
            cell.setCellType(HSSFCell.CELL_TYPE_STRING);
            cell.setCellValue((String) cswhObjList.get(j).getCsbm());

            cell = row.createCell(3);
            cell.setCellType(HSSFCell.CELL_TYPE_STRING);
            cell.setCellValue((String) cswhObjList.get(j).getRemark());

            cell = row.createCell(4);
            cell.setCellType(HSSFCell.CELL_TYPE_STRING);
            if (cswhObjList.get(j).getJczt().equals("C")) {
                cell.setCellValue("法人");
            } else if (cswhObjList.get(j).getJczt().equals("P")) {
                cell.setCellValue("自然人");
            } else {
                cell.setCellValue("所有");
            }

            cell = row.createCell(5);
            cell.setCellType(HSSFCell.CELL_TYPE_STRING);
            cell.setCellValue(cswhObjList.get(j).getCssdValue());

            cell = row.createCell(6);
            cell.setCellType(HSSFCell.CELL_TYPE_STRING);
            cell.setCellValue(cswhObjList.get(j).getYj());

            cell = row.createCell(7);
            cell.setCellType(HSSFCell.CELL_TYPE_STRING);
            if (cswhObjList.get(j).getJclb().equals("C")) {
                cell.setCellValue("处罚");
            } else {
                cell.setCellValue("激励");
            }

            count++;
        }

        cswhObjList = null;
        try {
            fOut = response.getOutputStream();
            workbook.write(fOut);
            workbook.close();
            fOut.flush();
            fOut.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
